package cn.tristenstudy.blog.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 *
 * @author xichen1.li
 * @date 2021-01-26 11:01
 */
@Configuration
public class RabbitMQConfig {

    @Value("${exchange.name}")
    private String exchangeName;
    @Value("${queue.name}")
    private String queueName;

    /**
     * 交换机
     *
     * @return 交换机
     */
    @Bean("bootExchange")
    public Exchange bootExchange() {
        return ExchangeBuilder.topicExchange(exchangeName).durable(true).build();
    }

    /**
     * Queue队列
     *
     * @return Queue队列
     */
    @Bean("bootQueue")
    public Queue bootQueue() {
        return QueueBuilder.durable(queueName).build();
    }

    /**
     * 队列和交换机绑定关系 Binding
     *
     * @param queue    队列
     * @param exchange 交换机
     * @return 队列和交换机绑定关系 Binding
     */
    @Bean
    public Binding bindingQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("email.#").noargs();
    }
}
